$(function () {
  $('.select-box button').each((index, item) => {
    const name = $(item).attr('name');
    // console.log('name', name);
    // $(item).click(name);
    switch (name) {
      case 'getSelection':
        $(item).click(selection)
        break;
      case 'selectAll':
        $(item).click(selectAll)
        break;
      case 'selectRange':
        $(item).click(selectRange)
        break;
      case 'selectMutiple':
        $(item).click(selectMutiple)
        break;
    }
  });

  const editor = $('#editor')[0];
  function selection() {
    const selection = getSelection();
    const range = selection.getRangeAt(0);
    console.dir(range);
  }
  function selectAll() {
    const range = document.createRange();
    range.selectNodeContents(editor);
    const selection = getSelection();
    selection.removeAllRanges();
    selection.addRange(range)
  }
  function selectRange() {
    var range2 = document.createRange();
    var startNode = editor.firstChild;
    range2.setStart(startNode, 1);
    range2.setEnd(startNode, 7);
    const selection = getSelection();
    selection.removeAllRanges();
    selection.addRange(range2)
  }
  function selectMutiple() {
    const range = document.createRange();
    const startNode = editor.childNodes[1].firstChild
    range.setStart(startNode, 1);
    const endNode = editor.childNodes[2].firstChild
    range.setEnd(endNode, 3);
    const selection = getSelection();
    selection.removeAllRanges();
    selection.addRange(range)
  }
});
